package com.alipay.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayRequest;
import com.alipay.api.AlipayResponse;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.alipay.config.AlipayConfig;
import com.alipay.constant.ProductCode;
import com.alipay.model.request.WapPayRequest;
import com.alipay.model.response.PcPayResponse;
import com.alipay.model.response.WapPayResponse;
import com.alipay.service.AbstractPayService;

/**
 * @author lwj
 * @create 2025-10-14-19:51
 */
public class WapPayServiceImpl extends AbstractPayService<WapPayRequest, WapPayResponse> {
    public WapPayServiceImpl(AlipayClient alipayClient, AlipayConfig alipayConfig) {
        super(alipayClient, alipayConfig);
    }

    @Override
    protected AlipayRequest createAlipayRequest() {
        return new AlipayTradeWapPayRequest();
    }

    @Override
    protected void setRequestParams(AlipayRequest alipayRequest, JSONObject bizContent) {
        // 强转为具体请求类型
        AlipayTradeWapPayRequest wapPayRequest = (AlipayTradeWapPayRequest) alipayRequest;
        // 将 JSON 类型转换为 Model 型
        AlipayTradeWapPayModel model = bizContent.toJavaObject(AlipayTradeWapPayModel.class);
        // 设置请求参数
        wapPayRequest.setBizModel(model);
    }

    @Override
    protected AlipayResponse executeAlipayRequest(AlipayRequest alipayRequest) throws AlipayApiException {
        // 执行请求
        return alipayClient.pageExecute((AlipayTradeWapPayRequest) alipayRequest);
    }

    @Override
    protected void setCallbackUrl(AlipayRequest alipayRequest) {
        // 设置同步与异步的回调地址
        alipayRequest.setNotifyUrl(alipayConfig.getNotifyUrl());
        alipayRequest.setReturnUrl(alipayConfig.getReturnUrl());
    }

    @Override
    protected WapPayResponse handleAlipayResponse(AlipayResponse alipayResponse, WapPayRequest request) {
        WapPayResponse response = new WapPayResponse();
        // 获取响应结果
        AlipayTradeWapPayResponse wapPayResponse = (AlipayTradeWapPayResponse) alipayResponse;

        // 填充自定义响应对象数据
        return populateResponse(response,wapPayResponse);
    }

    @Override
    protected WapPayResponse createEmptyResponse() {
        return new WapPayResponse();
    }

    @Override
    protected String getProductCode() {
        return ProductCode.WAP_PAY;
    }

    @Override
    protected void populateSpecificFields(WapPayResponse response, AlipayResponse alipayResponse) {
        // 强转为PC支付响应
        AlipayTradeWapPayResponse wapPayResponse = (AlipayTradeWapPayResponse) alipayResponse;
        // 设置订单信息
        response.setOrderHtmlForm(wapPayResponse.getBody());
    }
}
